#include "gestionnaireSignets.h"
#include "FenPrincipale.h"
#include <QtDebug>
#include <QRegExp>
#include <QFile>
#include <QDomElement>
#include <QWebSettings>
#include <QAction>

GestionnaireSignets::GestionnaireSignets(QObject * parent) : QObject(parent) {
    QWebSettings::setIconDatabasePath("./");
    signalMapperSignets = new QSignalMapper(this);
    recupListe();
}

void GestionnaireSignets::recupListe() {
    listeSig.clear();
    QDomDocument doc("BookmarksML");
    QFile file("bookmarks.xml");
    if (!file.open(QIODevice::ReadOnly))
        return;
    if (!doc.setContent(&file)) {
        file.close();
        return;
    }
    file.close();
    QDomElement root = doc.documentElement();
    if (root.tagName() != "bookmarks")
        return;
    QDomNode n = root.firstChild();
    while (!n.isNull()) {
        QDomElement e = n.toElement();
        if (!e.isNull()) {
            Signet *sig = new Signet;
            sig->setNom(e.attribute("name", ""));
            sig->setUrl(e.attribute("url", "about:blank"));
            sig->setFavicon(QWebSettings::iconForUrl(sig->getUrl()));
            listeSig.append(sig);
        }

        n = n.nextSibling();
    }

    for (int i = 0; i < listeSig.size(); i++) {
        QAction *act = new QAction(listeSig.at(i)->getIcon(), listeSig.at(i)->getNom(), qobject_cast<FenPrincipale*>(parent())->menuSignets);
        act->setStatusTip(listeSig.at(i)->getUrl().toString());
        connect(act, SIGNAL(triggered()), signalMapperSignets, SLOT(map()));
        signalMapperSignets->setMapping(act, listeSig.at(i)->getUrl().toString());
        connect(signalMapperSignets, SIGNAL(mapped(const QString &)), qobject_cast<FenPrincipale*>(parent()), SLOT(chargerUrl(const QString &)));
        qobject_cast<FenPrincipale*>(parent())->menuSignets->addAction(act);
    }
}

void GestionnaireSignets::ecrireListe() {
    QFile file("bookmarks.xml");
    if (!file.open(QIODevice::WriteOnly))
        return;
    QTextStream in(&file);
    in << "<!DOCTYPE BookmarksML>\n";
    in << "<bookmarks>\n";
    for (int i = 0; i < listeSig.size(); i++) {
        in << "\t<e" << QString::number(i);
        in << " name=\"" << listeSig.at(i)->getNom() << "\"";
        in << " url=\"" << listeSig.at(i)->getUrl().toString() << "\" />\n";
    }
    in << "</bookmarks>";
    file.close();
}

void GestionnaireSignets::ajouterSignet(Signet* pSig) {
    bool cap = false;
    int nbCap = 0;
    QRegExp reg;
    QString regExp;
    regExp = "^";
    regExp.append(pSig->getNom());
    regExp.append("\\(([0-9]*)\\)$");
    reg.setPattern(regExp);
    for (int i = 0; i < listeSig.size(); i++) {
        if (reg.exactMatch(listeSig.at(i)->getNom())) {
            cap = true;
            if (nbCap < reg.cap(1).toInt())
                nbCap = reg.cap(1).toInt();
        }
    }
    if (cap)
        pSig->setNom(pSig->getNom().append("(").append(QString::number(nbCap + 1)).append(")"));
    else {
        for (int i = 0; i < listeSig.size(); i++) {
            while (listeSig.at(i)->getNom() == pSig->getNom()) {
                pSig->setNom(pSig->getNom().append("(2)"));
            }
        }
    }

    listeSig.append(pSig);
    qDebug() << "Signet ajouté : " << pSig->getNom() << " - " << pSig->getUrl().toString();
    ecrireListe();
    recupListe();
}
